/*
 * Copyright 2020 edenrun.cn All right reserved. This software is the
 * confidential and proprietary information ofedenrun.cn ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with edenrun.cn.
 */

package com.zoom.pluto.openapi.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zoom.pluto.common.model.dto.SmartDTO;
import com.zoom.pluto.common.model.dto.SmfDTO;
import com.zoom.pluto.common.model.dto.ZxgDTO;
import com.zoom.pluto.common.util.HttpClientUtils;
import com.zoom.pluto.openapi.service.OpenApiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 对外开放平台服务实现
 *
 * @author zoom
 * @date 2020-06-28
 */
@Service
@Slf4j
public class OpenApiServiceImpl implements OpenApiService {

    private final static String ZXG_URL = "http://192.168.192.4:8080/zxg/query/idno/%s";

    private final static String SMART_URL = "http://192.168.192.3:8080/smart/query/idno/%s";

    private final static String SMF_URL = "http://192.168.192.4:8081/smf/query/idno/%s";

    @Override
    public ZxgDTO getLastZxgInfo(String idNo) {
        try {
            String url = String.format(ZXG_URL, idNo);
            log.info("请求zxg地址-{}", url);
            String result = HttpClientUtils.doGet(url, null);
            log.info("获取zxg信息成功-{}", result);
            return JSON.parseObject(JSON.toJSONString(JSONObject.parseObject(result).get("resultContent")), ZxgDTO.class);
        } catch(Exception e) {
            log.error("获取ZXG信息异常", e);
            return new ZxgDTO();
        }
    }

    @Override
    public SmartDTO getLastSmartInfo(String idNo) {
        try {
            String url = String.format(SMART_URL, idNo);
            log.info("请求smart地址-{}", url);
            String result = HttpClientUtils.doGet(url, null);
            log.info("获取smart信息成功-{}", result);
            return JSON.parseObject(JSON.toJSONString(JSONObject.parseObject(result).get("resultContent")), SmartDTO.class);
        } catch(Exception e) {
            log.error("获取SMART信息异常", e);
            return new SmartDTO();
        }
    }

    @Override
    public SmfDTO getLastSmfInfo(String idNo) {
        try {
            String url = String.format(SMF_URL, idNo);
            log.info("请求smf地址-{}", url);
            String result = HttpClientUtils.doGet(url, null);
            log.info("获取smf信息成功-{}", result);
            return JSON.parseObject(JSON.toJSONString(JSONObject.parseObject(result).get("resultContent")), SmfDTO.class);
        } catch(Exception e) {
            log.error("获取SMF信息异常", e);
            return new SmfDTO();
        }
    }

}
